package model;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;

import org.apache.commons.collections.buffer.CircularFifoBuffer;

import client.PropertiesPopup;

import customLayouts.VerticalFlowLayout;

public class Sensor {
	private double analogValue;
	private String binaryValue;
	private double upperLimit, lowerLimit;
	private String name;
	private String type;
	private String unit;
	private String timeStamp;
	private String lowerAlarmName;
	private String upperAlarmName;
	private String alarmName;
	private CircularFifoBuffer values;
	
	
	public Sensor(String name, String type, double value, String unit, String timeStamp, double upperLimit, double lowerLimit, String lowerAlarmName, String upperAlarmName){
		this.upperLimit = upperLimit;
		this.lowerLimit = lowerLimit;
		this.name = name;
		this.type = type;
		this.unit = unit;
		this.timeStamp = timeStamp;
		this.lowerAlarmName = lowerAlarmName;
		this.upperAlarmName = upperAlarmName;
		this.alarmName = "nvt";
		this.analogValue = value;
		values = new CircularFifoBuffer(30);
	}
	
	public Sensor(String name, String type, String value, String timeStamp, String alarmName){
		this.name = name;
		this.type = type;
		this.timeStamp = timeStamp;
		this.binaryValue = value;
		this.alarmName = alarmName;
		values = new CircularFifoBuffer(30);
	}
	
	public void updateSensor(double value){
		this.analogValue = value;
		values.add(value);
	}
	
	public void updateSensor(String value){
		this.binaryValue = value;
		values.add(value);
	}
	
	public JComponent drawOnScreen(){
    	Border lineBorder = BorderFactory.createLineBorder(Color.BLACK);
    	final JPanel panel = new JPanel(false); 
    	panel.setLayout(new VerticalFlowLayout());
        panel.setBorder(lineBorder);
  
        JLabel sensortext = new JLabel(name);
        JLabel sensorvalue = new JLabel(Double.toString(analogValue) + "\u00b0C");
        
        //this should be done by a click on the entire block, but for now there is a button
        JButton prop = new JButton("properties");
        prop.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				new PropertiesPopup(Sensor.this);				
			}
		});
        //end of properties button
        
        JPanel sensValuePanel = new JPanel(false);
        sensValuePanel.setBorder(lineBorder);
        if(analogValue>upperLimit && analogValue>lowerLimit){
        	sensValuePanel.setBackground(Color.RED);
        } else {
        	sensValuePanel.setBackground(Color.GREEN);
        }
        
        sensValuePanel.add(sensorvalue);
        
        panel.add(sensortext);
        panel.add(sensValuePanel);
        panel.add(prop);
        
        return panel;	    
	}
	
	public double getValue(){
		return analogValue;
	}
	
	public void setupperLimit(int Val){
		upperLimit = Val;
	}
	
	public void setlowerLimit(int Val){
		lowerLimit = Val;
	}
	
	public double getupperLimit(){
		return upperLimit;
	}
	
	public double getlowerLimit(){
		return lowerLimit;
	}
	
	public boolean isRisky(){
		if(analogValue > lowerLimit && analogValue < upperLimit){
			return false;
		}
		else return true;
	}
	
	public String getName(){
		return name;
	}
	
	public void saveData(){
		values.add(analogValue);
	}
	
	public String getLast10(){
		String result = "";
		Object[] valueArray = values.toArray();
		for(int i = 0; i<valueArray.length;i++){
			Double resultDouble = (Double) valueArray[i];
			result = result + "," + resultDouble.intValue();
		}
		return result;
	}
	
	public int getMaximum(){
		int result = 0;
		Object[] valueArray = values.toArray();
		for(int i = 0; i<valueArray.length;i++){
			Double resultDouble = (Double) valueArray[i];
			int iteratedValue = resultDouble.intValue();
			if(iteratedValue>result){
				result = iteratedValue;
			}
		}
		return result;
	}
	
	public int getMinimum(){
		int result = Integer.MAX_VALUE;
		Object[] valueArray = values.toArray();
		for(int i = 0; i<valueArray.length;i++){
			Double resultDouble = (Double) valueArray[i];
			int iteratedValue = resultDouble.intValue();
			
			if(iteratedValue<result){
				result = iteratedValue;
			}
		}
		return result;
	}
	
	public int getAverage(){
		
		int sum = 0;
		Object[] valueArray = values.toArray();
		for(int i = 0; i<valueArray.length;i++){
			Double resultDouble = (Double) valueArray[i];
			sum += resultDouble.intValue();
		}
		if (valueArray.length == 0) {
			return 0;
		}
		return sum/valueArray.length;
	}
	
	public CircularFifoBuffer getBuffer(){
		return values;
	}

	public String getType() {
		return type;
	}

	public String getUnit() {
		return unit;
	}

	public String getLowerAlarmName() {
		return lowerAlarmName;
	}
	
	public String getUpperAlarmName() {
		return upperAlarmName;
	}
	
	public String getAlarmName() {
		return alarmName;
	}

	public String getTimeStamp() {
		return timeStamp;
	}
}

